package com.platform.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.dao.HealthDataDao;
import com.platform.dao.UserDao;
import com.platform.entity.HealthDataEntity;
import com.platform.entity.UserEntity;
import com.platform.utils.RRException;

/**
 * 健康指标数据Service实现类
 *
 * @author LarkerJin
 * @email 277901879@qq.com
 * @date 2019-10-14 21:46:52
 */
@Service("healthDataService")
public class HealthDataService {
	public static final Pattern pattern = Pattern.compile("[^0-9.\\.]");

	@Autowired
    private HealthDataDao healthDataDao;
    @Autowired
    private UserDao userDao;

    public List<HealthDataEntity> saveFromJson(JSONObject res,Integer userId){
    	String uuid = UUID.randomUUID().toString().replaceAll("-","");
    	String nickName = null;
//    	-1 未达标，1 达标，0 需关注
    	Integer explain = -1;
		if(res.has("words_result")) {
    		JSONArray jsonArray = res.getJSONArray("words_result");
    		for(int i=1; i<jsonArray.length(); i++) {
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("身体得分")>=0) {
    				nickName = jsonArray.getJSONObject(i-1).getString("words");
    				if(userId == null) {
    					Map<String,Object> params = new HashMap<String,Object>();
    					List<UserEntity> users = userDao.queryList(params);
    					if(users == null || users.size() == 0) {
    						throw new RRException("无该会员信息，请核实是否修改昵称！");
    					}
    					if(users.size() > 1) {
    						throw new RRException("多个会员昵称重名，无法匹配！");
    					}
    					userId = users.get(0).getId();
    				}
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				
    				healthData.setTargetName("体重");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i + 4).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 5).getString("words"));
			        matcher = pattern.matcher(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(Integer.valueOf(matcher.replaceAll("")));
    				healthDataDao.save(healthData);
    				i = i+6;
    				continue;
    			}
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("未达标指标")>=0) {
    				explain = -1;
    				continue;
    			}
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("需关注指标")>=0) {
    				explain = 0;
    				continue;
    			}
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("健康指标")>=0) {
    				explain = 1;
    				continue;
    			}
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("BM")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("BMI");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("BM")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("BMI");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("体脂")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("体脂");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("水分")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("水分");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("蛋白质")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("蛋白质");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("基础代谢")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("基础代谢");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("肌肉")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("肌肉");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("内脏脂肪")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("内脏脂肪");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("骨量")>=0) {
    				HealthDataEntity healthData = new HealthDataEntity();
    				healthData.setUserId(userId);
    				healthData.setUuid(uuid);
    				healthData.setNickName(nickName);
    				healthData.setTargetName("骨量");
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				healthData.setTargetData(BigDecimal.valueOf(Double.valueOf(data)));
    				healthData.setDescription(jsonArray.getJSONObject(i + 1).getString("words"));
    				healthData.setExplain(explain);
    				healthDataDao.save(healthData);
    				i = i+1;
    				continue;
    			}
    		}
		}
		
		return null;
    }
    
    public static void main(String[] args) {
        Matcher matcher = pattern.matcher("123a1.23");
        String data = matcher.replaceAll("");
        System.out.print(data);
	}
    
    public HealthDataEntity queryObject(Integer id) {
        return healthDataDao.queryObject(id);
    }

    public List<HealthDataEntity> queryList(Map<String, Object> map) {
        return healthDataDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return healthDataDao.queryTotal(map);
    }

    public int save(HealthDataEntity healthData) {
        return healthDataDao.save(healthData);
    }

    public int update(HealthDataEntity healthData) {
        return healthDataDao.update(healthData);
    }

    public int delete(Integer id) {
        return healthDataDao.delete(id);
    }

    public int deleteBatch(Integer[] ids) {
        return healthDataDao.deleteBatch(ids);
    }
}
